package org.oschina.api.discuss.discuss.controller;

import org.oschina.api.BaseController;
import org.oschina.api.discuss.discuss.model.Discuss;
import org.oschina.api.discuss.discuss.service.DiscussService;
import org.oschina.api.discuss.discuss.vo.DiscussAccessVo;
import org.oschina.api.discuss.discuss.vo.DiscussIndexDataVo;
import org.oschina.api.discuss.discuss.vo.DiscussVo;
import org.oschina.api.report.vo.ReportVo;
import org.oschina.bean.Pager;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.handler.validator.filter.HtmlCleanFileter;
import org.oschina.handler.validator.filter.TextOnlyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=Config.API_PATH+"/discuss")
public class DiscussController extends BaseController {
	
	@Autowired
	private DiscussService discussService;
	
	/**
	 * 举报
	 * @param id
	 * @param content
	 * @return
	 */
	@Validate
	@RequestMapping(value="/report/{id}",method=RequestMethod.POST)
	public Result createReport
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id,
		String content
	)
	{
		return discussService.createReport(id, content);
	}
	
	/**
	 * 返回举报详细信息
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/report/concrete/{id}",method=RequestMethod.GET)
	public ReportVo findReportConcrete
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return discussService.findReportConcrete(id);
	}
	
	/**
	 * 返回举报
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/report",method=RequestMethod.GET)
	public Pager<DiscussVo> findReport(Integer pn)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return discussService.findReport(pn);
	}
	
	/**
	 * 关注
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/favorite/create",method=RequestMethod.POST)
	public Result createFavorite
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return discussService.createFavorite(id);
	}
	
	/**
	 * 删除关注
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/favorite/delete",method=RequestMethod.POST)
	public Result deleteFavorite
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return discussService.deleteFavorite(id);
	}
	
	/**
	 * 根据搜索返回
	 * @param keywords
	 * @param first
	 * @param max
	 * @return
	 */
	@Validate
	@RequestMapping(value="/search",method=RequestMethod.GET)
	public Pager<DiscussVo> findVoBySearch
	(
		@Validate(validateField=@ValidateField(filter={TextOnlyFilter.class})) String keyword,
		Integer pn
	)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return discussService.findVoBySearch(keyword, pn);
	}
	
	/**
	 * 返回审核
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/check",method=RequestMethod.GET)
	public Pager<DiscussVo> findCheck(Integer pn)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return discussService.findCheck(pn);
	}
	
	/**
	 * 审核通过
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/check/pass/{id}",method=RequestMethod.POST)
	public Result checkPass
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return discussService.checkPass(id);
	}
	
	/**
	 * 审核不通过
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/check/failure/{id}",method=RequestMethod.POST)
	public Result checkFailure
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return discussService.checkFailure(id);
	}
	
	/**
	 * 返回首页讨论
	 * @return
	 */
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public DiscussIndexDataVo getIndex()
	{
		return discussService.getIndex();
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public DiscussAccessVo findById
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return discussService.findById(id);
	}
	
	/**
	 * 根据板块返回
	 * @param forum
	 * @param pn
	 * @return
	 */
	@Validate
	@RequestMapping(value="/forum/{forum}",method=RequestMethod.GET)
	public Pager<DiscussVo> findByForum
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer forum,
		Integer pn
	)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return discussService.findByForum(forum, pn);
	}
	
	/**
	 * 根据软件返回
	 * @param forum
	 * @param pn
	 * @return
	 */
	@Validate
	@RequestMapping(value="/software/{software}",method=RequestMethod.GET)
	public Pager<DiscussVo> findBySoftware
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer software,
		Integer pn
	)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return discussService.findVoBySoftware(software, pn);
	}
	
	/**
	 * 创建
	 * @param discuss
	 * @return
	 */
	@Validate
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public Result create
	(
		@Validate(validateField=
		{
			@ValidateField(name="title",filter={TextOnlyFilter.class},validateValue=true,min=5,max=100),
			@ValidateField(name="content",filter={HtmlCleanFileter.class},validateValue=true,min=5),
			@ValidateField(name="forum",validateValue=true,min=1)
		}) Discuss discuss,
		@Validate(validateField=@ValidateField(notNull=false,validateValue=true,max=5,filter={TextOnlyFilter.class})) String tags[]
	)
	{
		return discussService.create(discuss, tags);
	}
	
	/**
	 * 发表评论
	 * @param id
	 * @param content
	 * @return
	 */
	@Validate
	@RequestMapping(value="/comment/create",method=RequestMethod.POST)
	public Result createComment
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id,
		String content,
		Integer quote
	)
	{
		if(quote==null || quote<0)
			quote=0;
		
		return discussService.createComment(id, content, quote);
	}
	
	/**
	 * 编辑
	 * @param id
	 * @param title
	 * @param content
	 * @return
	 */
	@Validate
	@RequestMapping(value="/edit/{id}",method=RequestMethod.POST)
	public Result edit
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id,
		@Validate(validateField=@ValidateField(filter={TextOnlyFilter.class},validateValue=true,min=5,max=100)) String title,
		@Validate(validateField=@ValidateField(filter={HtmlCleanFileter.class},validateValue=true,min=5)) String content
	)
	{
		return discussService.edit(id, title, content);
	}
	
	/**
	 * 移动板块
	 * @param id
	 * @param forum
	 * @return
	 */
	@Validate
	@RequestMapping(value="/moveForum",method=RequestMethod.POST)
	public Result moveForum
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id,
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer forum
	)
	{
		return discussService.moveForum(id, forum);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/delete/{id}",method=RequestMethod.POST)
	public Result delete
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return discussService.delete(id);
	}
	
	/**
	 * 返回已删除
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public Pager<DiscussVo> findDelete(Integer pn)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return discussService.findDelete(pn);
	}
	
	/**
	 * 恢复已删除
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/recover/delete/{id}",method=RequestMethod.POST)
	public Result recoverDelete
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return discussService.recoverDelete(id);
	}
	
	/**
	 * 更新排序
	 * @param id
	 * @param order
	 * @return
	 */
	@Validate
	@RequestMapping(value="/update/order/{id}",method=RequestMethod.POST)
	public Result updateOrder
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id,
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer order
	)
	{
		return discussService.updateOrder(id, order);
	}
}